﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace TouchExplorer.Controls
{
    /// <summary>
    /// Interaction logic for FilesDisplaySelector.xaml
    /// </summary>
    public partial class FilesDisplaySelector : Grid
    {
        private FrameworkElement _currentElement;
        private FrameworkElement _newElement;
        
        private ExplorerViewModel Model
        {
            get
            {
                return DataContext as ExplorerViewModel;
            }
        }

        public FilesDisplaySelector()
        {
            InitializeComponent();
            _currentElement = new FilesList();
            this.Children.Add(_currentElement);
            Loaded += new RoutedEventHandler(FilesList_Loaded);
        }

        void FilesList_Loaded(object sender, RoutedEventArgs e)
        {
            Model.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Model_PropertyChanged);
        }

        void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs eArg)
        {
            if (eArg.PropertyName == "Display")
            {
                ChangeDisplay();
            }
        }

        private Storyboard _stbd;

        private bool scrollTop;

        private void ChangeDisplay()
        {            
            if (_newElement == null)
            {
                if (Model.Display == ExplorerViewModel.DisplayType.List && _currentElement is FilesTiles)
                {
                    _newElement = new FilesList();
                }
                else if (Model.Display == ExplorerViewModel.DisplayType.Tiles && _currentElement is FilesList)
                {
                    _newElement = new FilesTiles();
                }

                if (_newElement != null)
                {
                    Children.Add(_newElement);
                    _newElement.DataContext = Model;
                    _stbd = new Storyboard();
                    _stbd.Duration = new Duration(TimeSpan.FromSeconds(0.8));

                    var offset = RenderSize.Width + 30;

                    string currentName;
                    string newName;
                    if (scrollTop)
                    {
                        currentName = this.Animate(_currentElement, 0, -offset);
                        newName = this.Animate(_newElement, offset, 0);                    
                    }
                    else
                    {
                        currentName= this.Animate(_currentElement, 0, offset);
                        newName = this.Animate(_newElement, -offset, 0);                    
                    }
                    var ressourceName = "Stb" + _stbd.GetHashCode();

                    Resources.Add(ressourceName, _stbd);
                    _stbd.Completed += (sender, e) =>
                        {
                            Resources.Remove(ressourceName);
                            this.UnregisterName(currentName);
                            this.UnregisterName(newName);

                            _stbd.Stop();
                            Children.Remove(_currentElement);
                            var display = _currentElement as IFileDisplay;
                            if (display != null)
                            {
                                display.Release();
                            }
                            _currentElement = _newElement;
                            _newElement = null;
                            _stbd.Children.Clear();
                            _stbd = null;
                            scrollTop = !scrollTop;
                        }; 
                    
                    _stbd.Begin();
                }
            }
        }

        private string Animate(FrameworkElement elt, double from, double to)
        {
            var translate = new TranslateTransform();
            elt.RenderTransform = translate;
            var name = "Translate" + translate.GetHashCode();
            this.RegisterName(name, translate);
            var easing = new CubicEase();
            easing.EasingMode = EasingMode.EaseOut;
            
            var animation = new DoubleAnimation();
            animation.EasingFunction = easing;
            animation.From = from;
            animation.To = to;
            Storyboard.SetTargetName(animation, name);
            Storyboard.SetTargetProperty(animation, new PropertyPath("X"));
            this._stbd.Children.Add(animation);
            
            return name;
        }

        private void AnimateOpacity(DependencyObject elt, double from, double to)
        {
            var animation = new DoubleAnimation();
            animation.From = from;
            animation.To = to;
            Storyboard.SetTarget(animation, elt);
            Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
            this._stbd.Children.Add(animation);
        }

        void stbd_Completed(object sender, EventArgs e)
        {
            _stbd.Stop();
            Children.Remove(_currentElement);
            var display = _currentElement as IFileDisplay;
            if (display != null)
            {
                display.Release();
            }
            _currentElement = _newElement;
            _newElement = null;
            _stbd.Children.Clear();
            _stbd = null;
        }
    }
}
